<%@page import="com.mconnect.msurvey.model.User"%>
<%@page import="com.mconnect.msurvey.controller.UserManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>mSurveys</title>
</head>
<body>
	<%
	String id = request.getParameter("id");
	UserManager man = new UserManager();	
	String currentTime = man.getCurrentTime();
	
	if( id != null){
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		if("login".equals(id)){
			User user = man.getUserIfRegister(login, password);
			if(user != null){
				session.setAttribute("user", user);
				man.updateLoginTime(currentTime, user.getId_user());
	%>
				<jsp:forward page="index.jsp"></jsp:forward>
		<%	} else{ %>
				<jsp:forward page="index.jsp">
					<jsp:param value="Niepoprawny login lub hasło!" name="message"/>
				</jsp:forward>
		<%}
		
		}else if("admin".equals(id)){
			User admin = man.getAdminIfRegister(login, password);
			if(admin != null){
				session.setAttribute("admin", admin);
				man.updateLoginTime(currentTime, admin.getId_user());
				%>
				<jsp:forward page="adminPanel.jsp"></jsp:forward>
			<%}else{ %>
				<jsp:forward page="loginAdminForm.jsp">
					<jsp:param value="Niepoprawny login lub hasło!" name="message"/>
				</jsp:forward>
			<%}
		}else if("logout".equals(id)){
			User user = (User)session.getAttribute("user");
			if(user != null){
				man.updateLogoutTime(currentTime, user.getId_user());
			}
			session.removeAttribute("user"); %>
			<jsp:forward page="index.jsp"></jsp:forward> 
		<%
		}else if("logoutAdmin".equals(id)){	
			User admin = (User)session.getAttribute("admin");
			if(admin != null){
				man.updateLogoutTime(currentTime, admin.getId_user());
			}
			session.removeAttribute("admin"); %>
			<jsp:forward page="adminPanel.jsp"></jsp:forward> 
		<%}
	}	%>
	
	
</body>
</html>